﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.Fact_Dim;
using SdmxMl.Common;

namespace SdmxMl.Panels
{
    public partial class DlgFactAndDim : Form
    {
        private SmManager sm;
        private KeyFamilyType dsd;
        private HierarchicalCodelistType hierarchyArtefact;
        private CodeListType codelistArtefact;
        private Artefact ar;
        private int LevelDepth;

        public DlgFactAndDim(SmManager s, Artefact a)
        {
            sm = s;
            ar = a;
            dsd = a as KeyFamilyType;
            hierarchyArtefact = a as HierarchicalCodelistType;
            codelistArtefact = a as CodeListType;

            InitializeComponent();

            buttonSave.Enabled = false;
            if (hierarchyArtefact != null)
                LevelDepth = hierarchyArtefact.HierarchyList[0].CalculateMaxLevelDepth();

            SetDisplayAccordingToArtefact();

        }

        private void SetDisplayAccordingToArtefact()
        {
            labelFinalLevel.Visible = false;
            numericUpDownFinalLevel.Visible = false;
            textBoxDsdFullId.Text = ar.FullIdent;
            textBoxNameDsd.Text = ar.Name.ToString();

            if (hierarchyArtefact != null)
            {
                labelFinalLevel.Visible = true;
                numericUpDownFinalLevel.Maximum = LevelDepth;
                numericUpDownFinalLevel.Value = LevelDepth;
                numericUpDownFinalLevel.Visible = true;
            }

        }

        /// <summary>Generate SQL Event </summary>
        /// <param name="sender"></param><param name="e"></param>
        private void buttonGenSql_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                   if (dsd != null)
                        GenSqlForDsd();
                    else if (hierarchyArtefact != null)
                        GenSqlForHierarchy();
                    else if (codelistArtefact != null)
                        GenSqlForCodelist();

                    buttonSave.Enabled = true;
                }
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "SQL", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        private void GenSqlForDsd()
        {
            richTextBoxSql.Text = string.Empty;

            CollectDsdInfo collectSql = new CollectDsdInfo(sm, dsd);

            richTextBoxSql.Text = collectSql.CreateOracleSql();
        }
        

        private void GenSqlForHierarchy()
        {
            richTextBoxSql.Text = string.Empty;

            int RequiredDepth = Convert.ToInt32(numericUpDownFinalLevel.Value);
            CollectHierarchyInfo collectSql = new CollectHierarchyInfo(
                new RefNomen(sm), hierarchyArtefact, RequiredDepth);

            richTextBoxSql.Text = collectSql.CreateOracleSql();
        }
        

        private void GenSqlForCodelist()
        {
            richTextBoxSql.Text = string.Empty;

            int RequiredDepth = Convert.ToInt32(numericUpDownFinalLevel.Value);
            CollectCodelistInfo collectSql = new CollectCodelistInfo(
                new RefNomen(sm), codelistArtefact);

            richTextBoxSql.Text = collectSql.CreateOracleSql();
        }
        

        /// <summary>
        ///  Save SQL script
        /// </summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName, false, Encoding.UTF8))
                    {
                        sw.Write(richTextBoxSql.Text);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "SQL", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        
    }
}
